package com.ythook.mentor.service;

import com.ythook.mentor.entity.*;
import com.ythook.mentor.repository.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class TrainingsService {
    @Resource
    private TrainingsRepository trainingsRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private MentorSkillsRepository mentorSkillsRepository;
    @Resource
    private MentorCalendarRepository mentorCalendarRepository;
    @Resource
    private TechnologiesRepository technologiesRepository;

    public List<Trainings> search() {
        return trainingsRepository.findAll();
    }

    public List<TrainingsDetail> searchAll() {
        List<TrainingsDetail> list_trainingsDetail = new ArrayList<>();
        List<Trainings> list_trainings = trainingsRepository.findAll();
        list_trainings.stream().forEach((s) -> {
            TrainingsDetail trainingsDetail = new TrainingsDetail();
            trainingsDetail.setTrainings(s);

            Optional<User> user = userRepository.findById(s.getMentorId());
            trainingsDetail.setUser(user);

            List<MentorSkills> list_mentorSkills = mentorSkillsRepository.findAllByMentorId(s.getMentorId());
            trainingsDetail.setListMentorSkills(list_mentorSkills);

            Optional<MentorCalendar> mentorCalendar = mentorCalendarRepository.findByMentorId(s.getMentorId());
            trainingsDetail.setMentorCalendar(mentorCalendar);

            list_trainingsDetail.add(trainingsDetail);
        });
        return list_trainingsDetail;
    }

    public Optional<Trainings> findById(Long id) {
        return trainingsRepository.findById(id);
    }


    public List<TrainingsDetail> findByUserId(Long userId, String trainingStatus) {
        List<Trainings> list_trainings = null;
        if(trainingStatus.equals("5")){
            list_trainings = trainingsRepository.findByUserIdAndTrainingsStatus(userId, trainingStatus);
        }else{
            list_trainings = trainingsRepository.findByUserIdAndTrainingsStatusNot(userId, "5");
        }
        List<TrainingsDetail> list_trainingsDetail = new ArrayList<>();
        list_trainings.stream().forEach((s) -> {
            TrainingsDetail trainingsDetail = new TrainingsDetail();
            trainingsDetail.setTrainings(s);

            Optional<User> user = userRepository.findById(s.getUserId());
            trainingsDetail.setUser(user);

            Optional<User> mentor = userRepository.findById(s.getMentorId());
            trainingsDetail.setMentor(mentor);

            List<MentorSkills> list_mentorSkills = mentorSkillsRepository.findAllByMentorId(s.getMentorId());
            list_mentorSkills.stream().forEach((t) -> {
                Optional<Technologies> technologies = technologiesRepository.findById(t.getTechnologyId());
                t.setName(technologies.get().getName());
            });
            trainingsDetail.setListMentorSkills(list_mentorSkills);

            Optional<MentorCalendar> mentorCalendar = mentorCalendarRepository.findByMentorId(s.getMentorId());
            trainingsDetail.setMentorCalendar(mentorCalendar);

            list_trainingsDetail.add(trainingsDetail);
        });
        return list_trainingsDetail;
    }

    public List<TrainingsDetail> findByMentorId(Long mentorId, String trainingStatus) {
        List<Trainings> list_trainings = null;
        if(trainingStatus.equals("5")){
            list_trainings = trainingsRepository.findByMentorIdAndTrainingsStatus(mentorId, trainingStatus);
        }else{
            list_trainings = trainingsRepository.findByMentorIdAndTrainingsStatusNot(mentorId, "5");
        }
        List<TrainingsDetail> list_trainingsDetail = new ArrayList<>();
        list_trainings.stream().forEach((s) -> {
            TrainingsDetail trainingsDetail = new TrainingsDetail();
            trainingsDetail.setTrainings(s);

            Optional<User> user = userRepository.findById(s.getUserId());
            trainingsDetail.setUser(user);

            Optional<User> mentor = userRepository.findById(s.getMentorId());
            trainingsDetail.setMentor(mentor);

            List<MentorSkills> list_mentorSkills = mentorSkillsRepository.findAllByMentorId(s.getMentorId());
            list_mentorSkills.stream().forEach((t) -> {
                Optional<Technologies> technologies = technologiesRepository.findById(t.getTechnologyId());
                t.setName(technologies.get().getName());
            });
            trainingsDetail.setListMentorSkills(list_mentorSkills);

            Optional<MentorCalendar> mentorCalendar = mentorCalendarRepository.findByMentorId(s.getMentorId());
            trainingsDetail.setMentorCalendar(mentorCalendar);

            list_trainingsDetail.add(trainingsDetail);
        });
        return list_trainingsDetail;
    }

    public List<Trainings> findByMentorId(Long mentorId) {
        List<Trainings> list_trainings = trainingsRepository.findByMentorId(mentorId);
        return list_trainings;
    }

    public void delete(Long id) {
        trainingsRepository.deleteById(id);
    }

    public Trainings add(Trainings trainings) {
        return trainingsRepository.save(trainings);
    }

    public Trainings update(Trainings trainings) {
        return trainingsRepository.save(trainings);
    }
}
